<?php
namespace app\index\controller;

use app\index\model\ContractCaiModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\SubbaoJieListModel;
use app\index\model\SubbaoJieModel;
use app\index\model\SubbaoShenListModel;
use app\index\model\SubbaoShenModel;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\SubbaoService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Subbao extends Common
{
    public function list_subbao_shen(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shen.deleted_user','=', 0];
        $where[] = ['shen.comid','=',$this->userinfo['comid']];
        $where[] = ['shen.isturn','=',1];
        $where[] = ['shen.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|shen.number|shen.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shen.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['shen.caiid','=', $param['caiid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shen.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shenmoney']) && !empty($param['shenmoney'])){
            $where[] = ['shen.shenmoney','like', '%'.$param['shenmoney'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shen.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shen.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shen.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shen.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = SubbaoShenModel::alias('shen')
            ->field('shen.*,e.name as enginname,cai.name as cainame,cai.gonger')
            ->join('engin e','e.id=shen.enginid')
            ->join('contract_cai cai','cai.id=shen.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('subbao_shen', 'cai'))
            ->order('shen.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('subbao_shen');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_subbao_shen(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['caiid'])){
                return outputJson(4000, '选择采购合同');
            }

            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['number'] = (new SubbaoService())->create_subbao_shen_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入申报明细');
            }


            Db::startTrans();
            try {
                $id = SubbaoShenModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shenid'] = $id;
                    SubbaoShenListModel::insertGetId($vv);
                }

                (new OptService())->relation_file($files,'subbao_shen',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'subbao_shen', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'subbao_shen', $id, '分包进度款申报');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new SubbaoService())->create_subbao_shen_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_subbao_shen(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SubbaoShenModel::where(['id'=>$param['id']])->find();


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入申报明细');
            }

            Db::startTrans();
            try {
                SubbaoShenModel::update($param);

                SubbaoShenListModel::where(['shenid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shenid'] = $param['id'];
                    SubbaoShenListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'subbao_shen',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'subbao_shen', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'subbao_shen', $param['id'], '分包进度款申报');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SubbaoShenModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;

            $list = SubbaoShenListModel::where(['shenid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('subbao_shen', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_subbao_shen(Request $request){
        $param = $request->post();
        $data = SubbaoShenModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            SubbaoShenModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'subbao_shen', $data['id'], '分包进度款申报');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_subbao_shen(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = SubbaoShenModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

        $list = SubbaoShenListModel::where(['shenid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('subbao_shen', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_subbao_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|jie.number|jie.money|jie.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['koufee']) && !empty($param['koufee'])){
            $where[] = ['jie.koufee','like', '%'.$param['koufee'].'%'];
        }
        if(isset($param['fafee']) && !empty($param['fafee'])){
            $where[] = ['jie.fafee','like', '%'.$param['fafee'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jie.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jie.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = SubbaoJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,cai.name as cainame,cai.gonger')
            ->join('engin e','e.id=jie.enginid')
            ->join('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('subbao_jie', 'cai'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('subbao_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_subbao_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['caiid'])){
                return outputJson(4000, '选择采购合同');
            }

            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['number'] = (new SubbaoService())->create_subbao_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入结算明细');
            }


            Db::startTrans();
            try {
                $id = SubbaoJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    SubbaoJieListModel::insertGetId($vv);
                }

                (new OptService())->relation_file($files,'subbao_jie',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'subbao_jie', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'subbao_jie', $id, '分包完工结算');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new SubbaoService())->create_subbao_jie_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_subbao_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = SubbaoJieModel::where(['id'=>$param['id']])->find();


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入结算明细');
            }

            Db::startTrans();
            try {
                SubbaoJieModel::update($param);

                SubbaoJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    SubbaoJieListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'subbao_jie',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'subbao_jie', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'subbao_jie', $param['id'], '分包完工结算');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SubbaoJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;

            $list = SubbaoJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('subbao_jie', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_subbao_jie(Request $request){
        $param = $request->post();
        $data = SubbaoJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            SubbaoJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'subbao_jie', $data['id'], '分包完工结算');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_subbao_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = SubbaoJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

        $list = SubbaoJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('subbao_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }









}
